#!/usr/bin/env python
# -*- coding:utf-8 -*-

# file:people.py
# author:wmf
# datetime:2021/5/23 22:33
# software: PyCharm
'''
this is function description
'''
# import module your need
from sqlalchemy import Column, String, create_engine, Integer, Sequence
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base

from homework10.homework10_4 import session, engine

Base = declarative_base()


class People(Base):
    __tablename__ = 'people'

    id = Column(Integer, primary_key=True, autoincrement=True)
    age = Column(Integer)
    name = Column(String(20))
    genda = Column(String(5))

    @classmethod
    def add_person(cls, **kwargs):
        try:
            people = People(
                id=kwargs.get('id'),
                name=kwargs.get('name'),
                age=kwargs.get('age'),
                genda=kwargs.get('genda')
            )

            session.add(people)
            session.commit()

        except Exception as e:
            session.rollback()
            print(e)
            return {'message': '数据库异常，更新失败', 'error': str(e)}

        return {'message': 'OK'}

    @classmethod
    def get_people(cls, **kwargs):
        try:
            students = session.query(cls).all()
            if not students:
                return {'message': '没有学生信息', 'error': '没有学生信息'}
            else:
                return students
        except Exception as e:
            return {'message': '数据库异常，获取学生信息失败', 'error': str(e)}

# 创建表
# Base.metadata.drop_all(engine)  # 删除base 所管理的所有mapping类
# Base.metadata.create_all(engine)
